﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.FileExpress.Components
{
    public partial class Attachment
    {
        #region Private members.

        protected int _ID;
        protected int _PackageID;
        protected string _OriginalFileName = string.Empty;
        protected string _FileName = string.Empty;
        protected long _FileSize;
        protected string _FileType = string.Empty;
        protected int _TotalDownloads;
        protected string _Url = string.Empty;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int PackageID
        {
            set { this._PackageID = value; }
            get { return this._PackageID; }
        }

        public string OriginalFileName
        {
            set { this._OriginalFileName = value; }
            get { return this._OriginalFileName; }
        }

        public string FileName
        {
            set { this._FileName = value; }
            get { return this._FileName; }
        }

        public long FileSize
        {
            set { this._FileSize = value; }
            get { return this._FileSize; }
        }

        public string FileType
        {
            set { this._FileType = value; }
            get { return this._FileType; }
        }

        public int TotalDownloads
        {
            set { this._TotalDownloads = value; }
            get { return this._TotalDownloads; }
        }

        public string Url
        {
            set { this._Url = value; }
            get { return this._Url; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Attachment Load(int iD)
        {
            string spName = "[dbo].p_FE_Attachment_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Attachment entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Attachment();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("PackageID"))) entity.PackageID = reader.GetInt32(reader.GetOrdinal("PackageID"));
                if (!reader.IsDBNull(reader.GetOrdinal("OriginalFileName"))) entity.OriginalFileName = reader.GetString(reader.GetOrdinal("OriginalFileName"));
                if (!reader.IsDBNull(reader.GetOrdinal("FileName"))) entity.FileName = reader.GetString(reader.GetOrdinal("FileName"));
                if (!reader.IsDBNull(reader.GetOrdinal("FileSize"))) entity.FileSize = reader.GetInt64(reader.GetOrdinal("FileSize"));
                if (!reader.IsDBNull(reader.GetOrdinal("FileType"))) entity.FileType = reader.GetString(reader.GetOrdinal("FileType"));
                if (!reader.IsDBNull(reader.GetOrdinal("TotalDownloads"))) entity.TotalDownloads = reader.GetInt32(reader.GetOrdinal("TotalDownloads"));
                if (!reader.IsDBNull(reader.GetOrdinal("Url"))) entity.Url = reader.GetString(reader.GetOrdinal("Url"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_PackageID(int packageID)
        {
            string spName = "[dbo].p_FE_Attachment_SelectBy_PackageID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@PackageID", SqlDbType.Int, packageID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_FE_Attachment_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_FE_Attachment_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_FE_Attachment_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_FE_Attachment_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int packageID, string originalFileName, string fileName, long fileSize, string fileType, int totalDownloads, string url)
        {
            Attachment entity = new Attachment();
            entity.PackageID = packageID;
            entity.OriginalFileName = originalFileName;
            entity.FileName = fileName;
            entity.FileSize = fileSize;
            entity.FileType = fileType;
            entity.TotalDownloads = totalDownloads;
            entity.Url = url;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_FE_Attachment_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@PackageID", SqlDbType.Int, this._PackageID);
            db.AddInParameter(dbCommand, "@OriginalFileName", SqlDbType.NVarChar, this._OriginalFileName);
            db.AddInParameter(dbCommand, "@FileName", SqlDbType.NVarChar, this._FileName);
            db.AddInParameter(dbCommand, "@FileSize", SqlDbType.BigInt, this._FileSize);
            db.AddInParameter(dbCommand, "@FileType", SqlDbType.VarChar, this._FileType);
            db.AddInParameter(dbCommand, "@TotalDownloads", SqlDbType.Int, this._TotalDownloads);
            db.AddInParameter(dbCommand, "@Url", SqlDbType.NVarChar, this._Url);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int packageID, string originalFileName, string fileName, long fileSize, string fileType, int totalDownloads, string url)
        {
            Attachment entity = new Attachment();
            entity.ID = iD;
            entity.PackageID = packageID;
            entity.OriginalFileName = originalFileName;
            entity.FileName = fileName;
            entity.FileSize = fileSize;
            entity.FileType = fileType;
            entity.TotalDownloads = totalDownloads;
            entity.Url = url;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_FE_Attachment_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@PackageID", SqlDbType.Int, this._PackageID);
            db.AddInParameter(dbCommand, "@OriginalFileName", SqlDbType.NVarChar, this._OriginalFileName);
            db.AddInParameter(dbCommand, "@FileName", SqlDbType.NVarChar, this._FileName);
            db.AddInParameter(dbCommand, "@FileSize", SqlDbType.BigInt, this._FileSize);
            db.AddInParameter(dbCommand, "@FileType", SqlDbType.VarChar, this._FileType);
            db.AddInParameter(dbCommand, "@TotalDownloads", SqlDbType.Int, this._TotalDownloads);
            db.AddInParameter(dbCommand, "@Url", SqlDbType.NVarChar, this._Url);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Attachment entity = new Attachment();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_FE_Attachment_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteBy_PackageID(int packageID)
        {
            string spName = "[dbo].p_FE_Attachment_DeleteBy_PackageID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@PackageID", SqlDbType.Int, packageID);

            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion
    }
}